package cn.tedu.csmall.passport.controller;

import cn.tedu.csmall.passport.pojo.dto.AdminAddNewDTO;
import cn.tedu.csmall.passport.pojo.dto.AdminLonginDTO;
import cn.tedu.csmall.passport.pojo.vo.AdminListItemVO;
import cn.tedu.csmall.passport.security.AdminDetails;
import cn.tedu.csmall.passport.service.IAdminService;
import cn.tedu.csmall.passport.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;

/**
 * ClassName: AdminController
 * Package: cn.tedu.csmall.passport.controller
 * Description 处理管理员相关请求控制类
 *
 * @version 0.0.1
 * @Author: 大傻
 * @Create:2022/12/8
 */
@Slf4j
@RestController
@RequestMapping("/admins")
@Api(tags = "01. 管理员模块")
public class AdminController {

    @Autowired
    private IAdminService iAdminService;

    public AdminController(){
        log.debug("创建管理员控制器对象: AdminController");
    }
    // http://localhost:9081/admins/login
    @ApiOperation("管理员登录")
    @ApiOperationSupport(order = 50)
    @PostMapping("/login")
    public JsonResult login(AdminLonginDTO adminLonginDTO){
        log.debug("开始处理【管理员登录】的请求，参数：{}", adminLonginDTO);
        iAdminService.login(adminLonginDTO);
        return JsonResult.ok();

    }

    // http://localhost:9081/admins/add-new
    @ApiOperation("添加管理员")
    @ApiOperationSupport(order = 100)
    @PreAuthorize("hasAnyAuthority('/ams/admin/add-new')")
    @PostMapping("/add-new")
    public JsonResult addNew(@Valid AdminAddNewDTO adminAddNewDTO) {
        log.debug("开始处理添加管理员的请求，参数：{}", adminAddNewDTO);
        iAdminService.addNew(adminAddNewDTO);
        return JsonResult.ok();
    }
    // http://localhost:9081/admins/9527/delete
    @ApiOperation("根据id删除删除管理员")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "管理员ID", required = true, dataType = "long")
    @PreAuthorize("hasAuthority('/ams/admin/delete')")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult delete(@PathVariable Long id,
                             @ApiIgnore @AuthenticationPrincipal AdminDetails adminDetails) {
        log.debug("开始处理【根据id删除删除管理员】的请求，参数：{}", id);
        log.debug("当事人：{}", adminDetails);
        log.debug("当事人的ID：{}", adminDetails.getId());
        log.debug("当事人的用户名：{}", adminDetails.getUsername());
        iAdminService.deleteById(id);
        return JsonResult.ok();
    }

    // http://localhost:9081/admins/1998/enable
    @ApiOperation("启动管理员")
    @ApiOperationSupport(order = 310)
    @PostMapping("/{id}/enable")
    public JsonResult setEnable(@PathVariable Long id){
        log.debug("开始处理【启用管理员】的业务，参数：{}", id);
        iAdminService.setEnable(id);
        return JsonResult.ok();
    }
    // http://localhost:9081/admins/1998/disable
    @ApiOperation("禁用管理员")
    @ApiOperationSupport(order = 311)
    @PostMapping("/{id}/disable")
    public JsonResult setDisable(@PathVariable Long id){
        log.debug("开始处理【禁用管理员】的业务，参数：{}", id);
        iAdminService.setDisable(id);
        return JsonResult.ok();
    }
    // http://localhost:9081/admins
    @ApiOperation("查询管理员列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("")
    public JsonResult list() {
        log.debug("开始处理【查询管理员列表】的请求，参数：无");
        List<AdminListItemVO> list = iAdminService.list();
        return JsonResult.ok(list);
    }



}
